<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import useStore from '@/store';
import { props } from './form';
defineOptions({ name: 'WarehouseForm' });
const _props = defineProps(props);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      window.$api.warehouse.save(form).then(res => {
        ElMessage.success('保存成功!');
        useStore.window.windowRemove(_props.window?.name);
        if (_props.window?.callBack) _props.window?.callBack();
      });
    }
  });
};
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  id: null,
  code: null,
  name: null,
  address: null,
  manageManName: null,
  telephone: null,
  attribute: null,
  remark: null,
});
const rules = reactive<FormRules>({
  code: [
    { required: true, message: '请输入仓库代码', trigger: 'blur' },
    { max: 50, message: '不能多于10个字符', trigger: 'blur' },
  ],
  name: [
    { required: true, message: '请输入仓库名称', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
  address: [
    { required: true, message: '请输入仓库地址', trigger: 'blur' },
    { max: 50, message: '不能多于80个字符', trigger: 'blur' },
  ],
  attribute: [
    { required: true, message: '请输入仓库属性', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
});
const detail = () => {
  window.$api.warehouse.detail(_props.window.params.id).then(res => {
    form.id = res.data.id;
    form.code = res.data.code;
    form.name = res.data.name;
    form.address = res.data.address;
    form.manageManName = res.data.manageManName;
    form.telephone = res.data.telephone;
    form.attribute = res.data.attribute;
    form.remark = res.data.remark;
  });
};
onMounted(() => {
  if (_props.window.params) {
    if (_props.window.params.id) {
      detail();
    }
  }
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save"></windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库代码" prop="code">
            <el-input v-model="form.code" placeholder="请输入仓库代码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入仓库名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入供应商代码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="管理人" prop="manageManName">
            <el-input v-model="form.manageManName" placeholder="请输入管理人" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="联系电话" prop="telephone">
            <el-input v-model="form.telephone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库属性" prop="attribute">
            <el-input v-model="form.attribute" placeholder="请输入仓库属性" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
